package cn.com.dycc.party_dues.info.controller;



import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.com.dycc.common.controller.BaseController;
import cn.com.dycc.party_dues.info.param.Pb_Party_DuesParam;
import cn.com.dycc.party_dues.info.service.Pb_Party_DuesNotFoundException;
import cn.com.dycc.party_dues.info.service.Pb_Party_DuesService;
import cn.com.dycc.util.JsonObjectResult;

/**
 * 党费信息表控制层
 * @author gaofan
 *
 */
@Controller
@RequestMapping("/party_dues/info")
public class Pb_Party_DuesController extends BaseController{
	
	@Resource
	private Pb_Party_DuesService pb_Party_DuesService;
	
	/**
	 * 保存上传的excel党费数据
	 * @param infos
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save.do")
	@ResponseBody
	public JsonObjectResult save(String infos,HttpSession session,HttpServletRequest request) throws Exception{
		
		int d=pb_Party_DuesService.saveExcel(infos, request);
		String flag="成功导入数据条数为:"+d;
		return new JsonObjectResult(flag);		
	}
	
	/**
	 * 读取上传excel党费数据并返回
	 * @param file
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/batchimport")
	@ResponseBody
    public JsonObjectResult batchimport(@RequestParam(value="file") MultipartFile file,
            HttpServletRequest request,HttpServletResponse response) throws IOException{
		//判断文件是否为空
        if(file==null) return new JsonObjectResult(JsonObjectResult.ERROR, "", "未获取上传文件", "");
        //获取文件名
        String name=file.getOriginalFilename();
        System.out.println("上传文件名:"+name);
        //进一步判断文件是否为空（即判断其大小是否为0或其名称是否为null）
        long size=file.getSize();
        if(name==null || ("").equals(name) && size==0) return new JsonObjectResult(JsonObjectResult.ERROR, "", "上传文件为空文件", "");
        return pb_Party_DuesService.batchImport(name,file);
	}
	
	/**
	 * 新增党费信息
	 * @param param
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/add.do")
	@ResponseBody
	public JsonObjectResult add(Pb_Party_DuesParam param,HttpSession session,HttpServletRequest request) throws Exception{
		
		int d=pb_Party_DuesService.add(param,request);
		String flag="成功导入数据条数为:"+d;
		return new JsonObjectResult(flag);		
	}
	
	/**
	 * 更新党费信息
	 * @param param
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/update.do")
	@ResponseBody
	public JsonObjectResult update(Pb_Party_DuesParam param,HttpServletRequest request,HttpSession session) throws Exception{
		
		int d=pb_Party_DuesService.update(param, request);
		String flag="成功更新数据条数为:"+d;
		return new JsonObjectResult(flag);
	}
	
	/**
	 * 删除党费信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete.do")
	@ResponseBody
	public JsonObjectResult delete(HttpServletRequest request) throws Exception{
		String []attrStrs=request.getParameterValues("dues_id");
		int d=pb_Party_DuesService.delete(attrStrs, request);
		String flag="成功删除数据条数为:"+d;
		return new JsonObjectResult(flag);
	}
	
	/**
	 * 查询登录用户的党费信息
	 * @param param
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list.do")
	@ResponseBody
	public JsonObjectResult findByParam(Pb_Party_DuesParam param,HttpSession session,HttpServletRequest request) throws Exception{
		String id=(String)session.getAttribute("id");
		param.setUser_id(id);	
		return pb_Party_DuesService.findByParam(param, request);
		
	}
	
	/**
	 * 未支付的党费信息
	 * @param param
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/unpaid.do")
	@ResponseBody
	public JsonObjectResult findUnpaidDuesByParam(Pb_Party_DuesParam param,HttpSession session,HttpServletRequest request) throws Exception{
		param.setPay_flag("0");	
		return pb_Party_DuesService.findByParam(param, request);
		
	}
	
	/**
	 * 查询特殊党费信息
	 * @param param
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/special.do")
	@ResponseBody
	public JsonObjectResult findSpecialDuesByParam(Pb_Party_DuesParam param,HttpSession session,HttpServletRequest request) throws Exception{
		param.setDues_type("1");	
		return pb_Party_DuesService.findByParam(param, request);
		
	}
	
	/**
	 * 有关用户本组织下所有的党费信息
	 * @param param
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listAll.do")
	@ResponseBody
	public JsonObjectResult findAllByParam(Pb_Party_DuesParam param,HttpSession session,HttpServletRequest request) throws Exception{
		
		return pb_Party_DuesService.findByParam(param, request);
		
	}
	
	/**
	 * 党费信息详情
	 * @param param
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/detail.do")
	@ResponseBody
	public JsonObjectResult findDetailByParam(Pb_Party_DuesParam param,HttpSession session,HttpServletRequest request) throws Exception{
		if(param.getDues_id()==null||param.getDues_id().trim().isEmpty()){
			throw new Pb_Party_DuesNotFoundException("请选择党费信息");
		}
		return pb_Party_DuesService.findByParam(param, request);
		
	}
	
	/**
	 * app党费信息查询(首页)
	 * @param param
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/applist.do")
	@ResponseBody
	public JsonObjectResult findAppDuesByParam(Pb_Party_DuesParam param,HttpSession session,HttpServletRequest request) throws Exception{
		
		return pb_Party_DuesService.findAppDuesByParam(param, request);
		
	}
	
	/**
	 * app党费手工收缴管理(首页)
	 * @param param
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/appUpaid.do")
	@ResponseBody
	public JsonObjectResult findAppUpaidDuesByParam(Pb_Party_DuesParam param,HttpSession session,HttpServletRequest request) throws Exception{
		param.setPay_flag("0");
		return pb_Party_DuesService.findAppDuesByParam(param, request);
		
	}
	
	/**
	 * app我的党费缴费(首页)--正常党费或困难减免党费
	 * @param param
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/appMyDues.do")
	@ResponseBody
	public JsonObjectResult findAppMyDuesByParam(Pb_Party_DuesParam param,HttpSession session,HttpServletRequest request) throws Exception{
		String id=(String)session.getAttribute("id");
		param.setUser_id(id);
		param.setExcept_dues_type("1");
		return pb_Party_DuesService.findAppMyDuesByParam(param, request);
		
	}
	
	/**
	 * app我的党费缴费(首页)--特殊党费
	 * @param param
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/appMySpecialDues.do")
	@ResponseBody
	public JsonObjectResult findAppMySpecialDuesByParam(Pb_Party_DuesParam param,HttpSession session,HttpServletRequest request) throws Exception{
		String id=(String)session.getAttribute("id");
		param.setUser_id(id);
		param.setDues_type("1");
		return pb_Party_DuesService.findAppMyDuesByParam(param, request);
		
	}
	
	/**
	 * app党费缴纳清单查询(首页)
	 * @param param
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/appDetail.do")
	@ResponseBody
	public JsonObjectResult findAppDetailDuesByParam(Pb_Party_DuesParam param,HttpSession session,HttpServletRequest request) throws Exception{
		
		return pb_Party_DuesService.findAppDetailDuesByParam(param, request);
		
	}
	
	/**
	 * app特殊党费的查询（管理员查所有特殊党费）
	 * @param param
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/appSpecial.do")
	@ResponseBody
	public JsonObjectResult findAppSpecialDuesByParam(Pb_Party_DuesParam param,HttpSession session,HttpServletRequest request) throws Exception{
		param.setDues_type("1");
		return pb_Party_DuesService.findAppDetailDuesByParam(param, request);
		
	}
	
	/*@RequestMapping("/appWarn.do")
	@ResponseBody
	public JsonObjectResult findAppWarnDuesByParam(Pb_Party_DuesParam param,HttpSession session,HttpServletRequest request) throws Exception{
		param.setExcept_pay_flag("2");
		return pb_Party_DuesService.findAppDetailDuesByParam(param, request);
		
	}*/
	
	/**
	 * 党费下订单
	 * @param param
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/unifiedOrder.do")
	@ResponseBody
	public JsonObjectResult findUnifiedOrderDuesByParam(String[] dues_ids,String pay_type,HttpSession session,HttpServletRequest request) throws Exception{
		System.out.println("dues_ids:"+dues_ids);
		if(dues_ids!=null){
			for(int i=0;i<dues_ids.length;i++){
				System.out.println("------------党费id------------");
				System.out.println(dues_ids[i]);
			}
		}
		return new JsonObjectResult(new Object());
		/*System.out.println("dues_ids:"+param.getDues_ids());
		if(param.getDues_ids()!=null&&param.getDues_ids().trim().isEmpty()){
			String [] dues_ids=StringUtil.getArray(param.getDues_ids());
			for(String dues_id:dues_ids){
				System.out.println("------------党费id------------");
				System.out.println(dues_id);
				
			}
		}*/
		
		
		
		//return pb_Party_DuesService.findUnifiedOrderDuesByParam(param, request);
		
	}
	/**
	 * 党费订单结果反馈
	 * @param param
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/resultOrder.do")
	@ResponseBody
	public JsonObjectResult findResultOrderDuesByParam(Pb_Party_DuesParam param,HttpSession session,HttpServletRequest request) throws Exception{
		
		return new JsonObjectResult(null);
		//return pb_Party_DuesService.findUnifiedOrderDuesByParam(param, request);
		
	}

}
